function openDB() {
	plus.sqlite.openDatabase({
		name: 'first',
		path: '_doc/note.db',
		success: function(e) {

		},
		fail: function(e) {

		}
	});
}

function createTable() {
	plus.sqlite.executeSql({
		name: 'first',
		sql: 'create table note(note_id INTEGER PRIMARY KEY AUTOINCREMENT ,title varchar(20),content TEXT,add_time DATETIME,update_time DATETIME,note_code TEXT)',
		success: function(e) {

		},
		fail: function(e) {

		}
	});
}

function insertData(note, callback) {
	console.log(note)
	plus.sqlite.executeSql({
		name: 'first', //这个属性一定要，否则没法执行sql语句
		sql: "insert into note('title','content','add_time','update_time','note_code') values('" +
			note.title + "','" + note.content + "','" + note.add_time + "','" + note.update_time + "','" + note
			.note_code + "')",
		success: function(e) {
			uni.showToast({
				title: '操作成功',
			});
			callback(1)
		},
		fail: function(e) {
			uni.showToast({
				title: '操作失败',
			});
			callback(2)
		},
	})
}

function deleteNote(note, callback) {
	plus.sqlite.executeSql({
		name: 'first',
		sql: "delete from note where note_id=" + note.note_id,
		success: function(e) {
			uni.showToast({
				title: "操作成功"
			});
			callback(1)
		},
		fail: function(e) {
			uni.showToast({
				icon: "none",
				title: "操作失败" + e.toString()
			});
			callback(e)
		}
	})
}

function updateData(note) {
	console.log(note)
	plus.sqlite.executeSql({
		name: 'first', //这个属性一定要，否则没法执行sql语句
		sql: "update note set title='" + note.title + "'," + "content='" + note.content + "'," +
			"update_time='" + note.update_time + "'where note_id=" + note.note_id,
		success: function(e) {
			uni.showToast({
				title: '操作成功',
			});
		},
		fail: function(e) {
			uni.showToast({
				title: '操作失败',
			});
		}
	})
}
/**
 * @param {Object} callback
 * @param {Object} keyWord
 * @param {Object} sort 1 按创建时间 2 按编辑时间 3 按标题名称
 */
function selectSQL(keyWord, sort, callback) {
	console.log(keyWord + sort)
	var sql = "select * from note ";
	if (keyWord != null && keyWord != "") {
		sql += "where title like '%" + keyWord + "%' or content like '%" + keyWord + "%' ";
	}
	if (sort == 1) {
		sql += "order by add_time desc";
	} else if (sort == 2) {
		sql += "order by update_time desc";
	} else if (sort == 3) {
		sql += "order by title desc";
	}
	console.log(sql)
	plus.sqlite.selectSql({
		name: 'first',
		sql: sql,
		success: function(e) {
			callback(e)
		},
		fail: function(e) {
			callback(e)
		}
	});
}

function droptable() {
	plus.sqlite.executeSql({
		name: 'first',
		sql: 'drop table note',
		success: function(e) {

		},
		fail: function(e) {

		}
	});
}

function closeDB() {
	plus.sqlite.closeDatabase({
		name: 'first',
		success: function(e) {

		},
		fail: function(e) {

		}
	});
}

function isOpenDB() {
	if (
		plus.sqlite.isOpenDatabase({
			name: 'first',
			path: '_doc/note.db'
		})
	) {

	} else {

	}
}

export {
	isOpenDB,
	openDB,
	createTable,
	insertData,
	selectSQL,
	droptable,
	closeDB,
	updateData,
	deleteNote
}
